package com.hxx.day7;

import org.junit.Test;

import java.util.TreeSet;

/**
 * treeSet 排序
 * 可以通过两个接口完成排序
 * 1.Comparable 接口
 * 2.Comparator 接口
 */
public class TestDemo4 {
    @Test
    public void test01(){
        TreeSet treeSet = new TreeSet();
        treeSet.add(300);
        treeSet.add(100);
        treeSet.add(600);
        treeSet.add(200);
        treeSet.add(500);
        System.out.println(treeSet);
    }
    @Test
    public void test02(){
        TreeSet treeSet = new TreeSet();
        treeSet.add(new Person(3,"zhangsan",18));
        treeSet.add(new Person(2,"lisi",20));
        treeSet.add(new Person(10,"wangwu",15));
        System.out.println(treeSet);
    }
}

class Person implements Comparable {
    private int id;
    private String name;
    private int age;

    public Person() {
    }

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Person){
            Person p = (Person) o;
            return  p.id - this.id;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

























